package cn.tom.controller;

import cn.tom.dao.OrderMapper;
import cn.tom.entity.OrdItem;
import cn.tom.entity.Order;
import cn.tom.service.BookService;
import cn.tom.service.OrderService;
import cn.tom.tool.Token;
import cn.tom.tool.XJson;
import cn.tom.tool.XResp;
import org.apache.catalina.AccessLog;
import org.apache.catalina.Lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/fore/ord")
public class OrderController {
    @Autowired
    OrderMapper orderMapper;
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    OrderService orderService;
    @Autowired
    BookService bookService;
    @PostMapping({"/addPay/{ordid}"})
    public XJson doPay(@PathVariable("ordid") Integer ordid) {
        int uid = Token.getUid();
        if (uid < 0) {
            return XResp.pack(401, "Token过期， 请重新登录.....", null);
        }
        try {
            int ret = this.orderService.doPay(ordid, 1);

            if (ret ==1 ) {
                log.info("hhhhhhhhhhhh");
                log.info("uid======="+ uid);
                List<Integer> bookidList = this.orderService.findBookidByOrdid(ordid);
                log.info("bookidList============" + bookidList);
                for (Integer bookid : bookidList) {
                    log.info("booooo======" + bookid);
                    int r = this.bookService.findRecord(uid, bookid);
                    log.info("r===========" + r);
                    if (r>0) {
                        continue;
                    }
                    this.bookService.addRecord(uid,bookid);
                }
                System.out.println("9999999999999999999999");
                return XResp.pack(200, "支付成功", null);
            }
        }catch (RuntimeException e) {
            return XResp.pack(2121, "支付失败，库存不足", null);
        }

        return XResp.pack(500, "支付失败", null);
    }

    @GetMapping({"/findByOrdid/{ordid}"})
    public XJson findByOrdid(@PathVariable("ordid") Integer ordid) {
        Order byOrdId = this.orderService.findByOrdId(ordid);
        log.info("===============this.orderService.findByOrdId(ordid)" + byOrdId);
        return XResp.pack(200, "", byOrdId);
    }

    @PostMapping({"/findPageOrders/{pagenum}/{lines}"})
    public XJson findOrdersByPages(Integer payStatus, Integer shipStatus, String days, @PathVariable("pagenum") Integer pagenum, @PathVariable("lines") Integer lines) {
        this.log.info("payStatus=" + payStatus + "\tshipStatus=" + shipStatus + "\tdays=" + days + "\tpagenum=" + pagenum + "\tlines=" + lines);
        HashMap<String, Object> map = new HashMap<>();
        int uid = Token.getUid();
        if (uid < 0) {
            return XResp.pack(401, "Token过期， 请重新登录.....", null);
        }
        map.put("uid", uid);
        if (payStatus != null && payStatus!=0) {
            map.put("payStatus", payStatus);
        }
        if (shipStatus != null && shipStatus!=0) {
            map.put("shipStatus", shipStatus);
        }
        int start = (pagenum - 1) * lines;
        if (start < 0) {
            start = 0;
        }
        map.put("start", start);
        map.put("lines", lines);
        if (days!="") {
            int days2 = Integer.parseInt(days);
            if (days2 > 0) {
                LocalDate currentDate = LocalDate.now();
                // 减去指定天数
                LocalDate modifiedDate = currentDate.minusDays(days2);
                // 将 LocalDate 转换为日期
                Date resultDate = Date.from(modifiedDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
                map.put("between",resultDate);
            }
        }
        this.log.info("查询条件:" + map);
        Object lst = this.orderService.findPageOrdersByUid(map);
        int total = this.orderService.getCountOrdersByUid(map);
        HashMap<String, Object> mm = new HashMap<>();
        mm.put("orders", lst);
        mm.put("total", total);
        return XResp.pack(200, "", mm);
    }

}
